#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll m, n;
int A[20];
ll d[20][10][2];

ll dp(int pos, int mod, int st, bool flg){
    if(pos == 0) return st==1 || mod==0;
    if(flg && d[pos][mod][st]) return d[pos][mod][st];
    int x = flg? 9:A[pos];
    ll ans = 0;
    for (int i = 0; i <= x; ++i) {
        if(i == 7 || st == 1)
            ans += dp(pos-1, (mod*10+i)%7, 1, flg||i<x);
        else ans += dp(pos-1, (mod*10+i)%7, st, flg||i<x);
    }
    if(flg) d[pos][mod][st] = ans;
    return ans;
}


ll cal(ll x){
    int p = 0;
    while (x){
        A[++p] = x%10;
        x/=10;
    }
    return dp(p, 0, 0, false);
}
int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);
    cin >> m >> n;
    ll f = cal(m)+n;
    ll l = m, r = 1e15;
    while (l <= r){
        ll mid = l + (r-l)/2;
        ll val = cal(mid);
        if(f > val)
            l = mid+1;
        else r = mid -1;
    }
    cout << l;
    return 0;
}